package com.pf.controller;

import com.pf.dto.BaseResult;
import com.pf.dto.request.JobAddReq;
import com.pf.dto.request.JobUpdateReq;
import com.pf.service.JobInfoService;
import com.pf.utils.BaseResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author PanFei
 * @version 1.0.0
 * @createTime 2021/8/10
 */
@RestController
@RequestMapping("/job/server")
public class JobInfoController {

    @Autowired
    private JobInfoService jobInfoService;


    /**
     * 添加定时任务
     *
     * @param jobInfo
     * @return
     */
    @PostMapping("/add")
    public BaseResult addJob(@RequestBody @Valid JobAddReq jobInfo) {
        jobInfoService.addJobInfo(jobInfo);
        return BaseResultUtils.success();
    }

    /**
     * 启用定时任务
     *
     * @param jobId
     * @return
     */
    @GetMapping("/start")
    public BaseResult startJob(@RequestParam("jobId") String jobId) {
        jobInfoService.startJob(jobId);
        return BaseResultUtils.success();
    }

    /**
     * 停用定时任务
     *
     * @param jobId
     * @return
     */
    @GetMapping("/stop")
    public BaseResult stopJob(@RequestParam("jobId") String jobId) {
        jobInfoService.stopJob(jobId);
        return BaseResultUtils.success();
    }

    /**
     * 修改任务信息
     *
     * @param jobReq
     * @return
     */
    @PostMapping("/updateJob")
    public BaseResult updateJob(@RequestBody @Valid JobUpdateReq jobReq) {
        jobInfoService.updateJobInfo(jobReq);
        return BaseResultUtils.success();
    }
}
